package com.taikang.system.service.impl;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.taikang.base.page.PageResult;
import com.taikang.system.dao.ISysRegionAreaDao;
import com.taikang.system.dao.ISysRegionDao;
import com.taikang.system.dto.SysRegionAreaCondition;
import com.taikang.system.dto.SysRegionCondition;
import com.taikang.system.entity.SysRegion;
import com.taikang.system.entity.SysRegionArea;
import com.taikang.system.form.SysRegionAreaForm;
import com.taikang.system.service.ISysRegionAreaService;

@Service("sysRegionAreaServiceImpl")
@Transactional(value = "transactionManager")
public class SysRegionAreaServiceImpl implements ISysRegionAreaService{

	@Resource(name = "sysRegionAreaDaoImpl")
	private ISysRegionAreaDao sysRegionAreaDao;
	@Resource(name = "sysRegionDaoImpl")
	private ISysRegionDao sysRegionDao;
	
	/**
	 * 分页查询
	 * @param pageResult 分页对象
	 * @param condition 查询条件类
	 */
	public void listSysRegionAreaByPage(PageResult<SysRegionArea> pageResult,SysRegionAreaCondition condition){
		int rows = sysRegionAreaDao.listSysRegionAreaByPageCount(condition);
		pageResult.setRows(rows);

		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<SysRegionArea> list = sysRegionAreaDao.listSysRegionAreaByPage(rowBounds,condition);
		pageResult.setResult(list);
	}

	/**
	 * 根据ID查询
	 * @param id 主键
	 * @return 大区管理
	 */
	public SysRegionArea getSysRegionAreaById(String id){
		return sysRegionAreaDao.getSysRegionAreaById(id);
	}

	/**
	 * 新增
	 * @param item 大区管理
	 */
	public void add(SysRegionAreaForm model){
		if(StringUtils.isBlank(model.getItem().getId())){
			sysRegionAreaDao.add(model.getItem());
		}else{
			List<SysRegion> sysRegionList = sysRegionDao.listSysRegionByAreaId(model.getItem().getId());
			Long[] ids = new Long[sysRegionList.size()];
			for (int i = 0 ; i < sysRegionList.size() ; i++ ) {
				ids[i] = sysRegionList.get(i).getId();
			}
			sysRegionDao.batchRemoveArea(ids);
			sysRegionAreaDao.update(model.getItem());
		}
		if (null != model.getRegionIds() && model.getRegionIds().length !=0) {
			sysRegionDao.batchFitArea(model.getRegionIds(), model.getItem().getId());
			List<Long> parentIdsList = Arrays.asList(model.getRegionIds());
			while(true){
				List<Long> childrenIdList = sysRegionDao.listSysRegionByParentId(parentIdsList);
				if (CollectionUtils.isEmpty(childrenIdList)) {
					break;
				}
				sysRegionDao.batchFitArea(childrenIdList.toArray(new Long[childrenIdList.size()]), model.getItem().getId());
				parentIdsList = new ArrayList<Long>();
				parentIdsList.addAll(childrenIdList);
			}
		}
		
	}

	/**
	 * 修改
	 * @param item 大区管理
	 */
	public void update(SysRegionArea item){
		sysRegionAreaDao.update(item);
	}

	/**
	 * 根据ID集合批量删除
	 * @param ids ID集合
	 */
	public void delByIds(String[] ids){
		sysRegionAreaDao.delByIds(ids);
	}

	@Override
	public List<SysRegionArea> listSysRegionAreaByAll() {
		List<SysRegionArea> sysRegionAreaList=sysRegionAreaDao.listSysRegionAreaByAll();
		List<SysRegionArea> newSysRegionAreaList =new ArrayList<SysRegionArea>();
		if(sysRegionAreaList!=null&&sysRegionAreaList.size()>0){
			for (SysRegionArea sysRegionArea : sysRegionAreaList) {
				SysRegionCondition condition =new SysRegionCondition();
				//大区
				condition.setFlag(1);
				condition.setAreaId(sysRegionArea.getId());
				//热门
				condition.setIsHot(1);
				//城市
				condition.setLevel(2);
				List<SysRegion> sysRegionList= sysRegionDao.listSysRegionAllByCondition(condition);
				if(sysRegionList!=null&& sysRegionList.size()>0){
					sysRegionArea.setSysRegionList(sysRegionList);
					newSysRegionAreaList.add(sysRegionArea);
				}
			}
		}
		return newSysRegionAreaList;
	}

}

